/**
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved. 
 */
package com.wilmar.process.entity.factory.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.wilmar.process.entity.factory.api.IBaseValueObjectFactory;
import com.wilmar.process.entity.vo.ValueObject;
import com.wilmar.process.enumerate.EnumValueObjectFactory;
import com.wilmar.process.enumerate.api.IValueObjectProperties;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0 May 12, 2010
 * @since 1.0.0
 * 
 */
public abstract class BaseValueObjectFactoryImpl<V extends ValueObject>
		implements IBaseValueObjectFactory<V> {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 1814669346123447040L;

	public static final IBaseValueObjectFactory<? extends ValueObject> getFactory(
			EnumValueObjectFactory factory) throws InstantiationException,
			IllegalAccessException {
		return factory.getFactoryInstance();
	}

	/**
	 * 获取记录集中对应字段的String值,如果该字段为空,则返回空字符串
	 * 
	 * @param resultSet
	 *            记录集
	 * @param vop
	 *            字段名称
	 * @return 对应的String值
	 * @throws SQLException
	 */
	protected String getString(ResultSet resultSet, IValueObjectProperties vop)
			throws SQLException {
		return this.getString(resultSet, vop, "");
	}

	/**
	 * 获取记录集中对应字段的String,如果该字段为空,则返回预先设定的默认值
	 * 
	 * @param resultSet
	 *            记录集
	 * @param vop
	 *            字段名称
	 * @param defaultValue
	 *            默认值
	 * @return 对应的String值
	 * @throws SQLException
	 */
	protected String getString(ResultSet resultSet, IValueObjectProperties vop,
			String defaultValue) throws SQLException {
		String result = resultSet.getString(vop.getColumnName());
		if (result == null)
			result = defaultValue;
		return result;
	}

	/**
	 * 获取记录集中对应字段的Long值,如果该字段为空,则返回空字符串
	 * 
	 * @param resultSet
	 *            记录集
	 * @param vop
	 *            字段名称
	 * @return 对应的Long值
	 * @throws SQLException
	 */
	protected Long getLong(ResultSet resultSet, IValueObjectProperties vop)
			throws SQLException {
		return this.getLong(resultSet, vop, 0L);
	}

	/**
	 * 获取记录集中对应字段的Long,如果该字段为空,则返回预先设定的默认值
	 * 
	 * @param resultSet
	 *            记录集
	 * @param vop
	 *            字段名称
	 * @param defaultValue
	 *            默认值
	 * @return 对应的Long值
	 * @throws SQLException
	 */
	protected Long getLong(ResultSet resultSet, IValueObjectProperties vop,
			Long defaultValue) throws SQLException {
		Long result = resultSet.getLong(vop.getColumnName());
		if (result == null)
			result = defaultValue;
		return result;
	}
}
